#import <EventKit/EventKit.h>
#import "CreativeWallpaperImageGenerator.h"
static CGFloat const kClockHeight = 160.0f;
static CGFloat const kSliderHeight = 90.0f;
static CGFloat const kListBackgroundInset = 20.0f;
static CGFloat const kListItemXInset = 15.0f;
static CGFloat const kListItemPadding = 2.0f;
static CGFloat const kItemBulletWidth = 5.0f;
@implementation CreativeWallpaperImageGenerator
+ (UIImage *)wallpaperImageWithBackground:(UIImage *)backgroundImage reminders:(NSArray *)reminders {
    CGRect screenBounds = [UIScreen mainScreen].bounds;
    CGFloat scale = [UIScreen mainScreen].scale;
    UIGraphicsBeginImageContextWithOptions(screenBounds.size, YES, scale);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGFloat horizontalImageScale = screenBounds.size.width / backgroundImage.size.width,
            verticalImageScale   = screenBounds.size.height / backgroundImage.size.height;
    CGRect imageRect;
    if (horizontalImageScale > verticalImageScale) {
        imageRect = CGRectMake(0.0f, 0.0f, backgroundImage.size.width * horizontalImageScale, backgroundImage.size.height * horizontalImageScale);
    } else {
        imageRect = CGRectMake(0.0f, 0.0f, backgroundImage.size.width * verticalImageScale, backgroundImage.size.height * verticalImageScale);
    }
    [self drawGradientBackgroundInContext:ctx
                           withStartColor:[UIColor colorWithRed:0.33f green:0.26f blue:0.43f alpha:1.0f]
                                 endColor:[UIColor colorWithRed:0.24f green:0.16f blue:0.36f alpha:1.0f]];
    CGFloat listBackgroundHeight = kListItemXInset * 2.0f;
    for (EKReminder *reminder in reminders) {
        CGRect reminderRect = [reminder.title boundingRectWithSize:CGSizeMake(screenBounds.size.width - (kListBackgroundInset * 2.0f) - (kListItemXInset * 2.0f), 9999.0f)
                                                           options:NSStringDrawingUsesLineFragmentOrigin
                                                        attributes:@{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:17.0f]}
                                                           context:nil];
        reminderRect.size.height += kListItemPadding * 2.0f;
        listBackgroundHeight += CGRectGetHeight(reminderRect);
    }
    CGFloat maxListHeight = screenBounds.size.height - kClockHeight - kSliderHeight;
    if (listBackgroundHeight > maxListHeight) {
        listBackgroundHeight = maxListHeight;
    }
    CGFloat listVerticalOffset = (maxListHeight - listBackgroundHeight) / 2.0f;
    CGRect listBackgroundRect = CGRectMake(kListBackgroundInset,
                                           kClockHeight + listVerticalOffset,
                                           screenBounds.size.width - kListBackgroundInset * 2.0f,
                                           listBackgroundHeight);
    UIBezierPath *listBackgroundPath = [UIBezierPath bezierPathWithRoundedRect:listBackgroundRect
                                                             byRoundingCorners:UIRectCornerAllCorners
                                                                   cornerRadii:CGSizeMake(10.0f, 10.0f)];
    CGContextAddPath(ctx, listBackgroundPath.CGPath);
    CGContextSetFillColorWithColor(ctx, [[UIColor colorWithWhite:1.0f alpha:0.8f] CGColor]);
    CGContextFillPath(ctx);
    __block CGFloat yOffset = 0.0f;
    [reminders enumerateObjectsUsingBlock:^(EKReminder *reminder, NSUInteger idx, BOOL *stop) {
        CGRect itemBulletRect = CGRectMake(CGRectGetMinX(listBackgroundRect) + kListItemXInset,
                                           CGRectGetMinY(listBackgroundRect) + kListItemXInset + yOffset + kItemBulletWidth * 1.5,
                                           kItemBulletWidth,
                                           kItemBulletWidth);
        UIBezierPath *itemBulletPath = [UIBezierPath bezierPathWithOvalInRect:itemBulletRect];
        CGContextBeginPath(ctx);
        CGContextAddPath(ctx, itemBulletPath.CGPath);
        CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor);
        CGContextFillPath(ctx);
        CGRect reminderRect = [reminder.title boundingRectWithSize:CGSizeMake(screenBounds.size.width - (kListBackgroundInset * 2.0f) - (kListItemXInset * 2.0f), 9999.0f)
                                                           options:NSStringDrawingUsesLineFragmentOrigin
                                                        attributes:@{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:17.0f]}
                                                           context:nil];
        reminderRect.origin = CGPointMake(CGRectGetMinX(listBackgroundRect) + kListItemXInset + (kItemBulletWidth * 2.0f),
                                          CGRectGetMinY(listBackgroundRect) + kListItemXInset + yOffset);
        reminderRect.size.height += kListItemPadding * 2.0f;
        yOffset += CGRectGetHeight(reminderRect);
        [reminder.title drawInRect:reminderRect
                    withAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:17.0f]}];
    }];
    CGImageRef img = CGBitmapContextCreateImage(ctx);
    UIGraphicsEndImageContext();
    UIImage *wallpaperImage = [UIImage imageWithCGImage:img];
    CGImageRelease(img);
    return wallpaperImage;
}
+ (void)drawGradientBackgroundInContext:(CGContextRef)context withStartColor:(UIColor *)startColor endColor:(UIColor *)endColor {
    CGFloat locations[2] = { 0.0, 1.0 };
    NSArray *colors = @[(id)startColor.CGColor,
                        (id)endColor.CGColor];
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (CFArrayRef)colors, locations);
    CGRect screenBounds = [UIScreen mainScreen].bounds;
    CGPoint startPoint = CGPointMake(0.0f, 0.0f), endPoint = CGPointMake(0.0f, CGRectGetMaxY(screenBounds));
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
}

- (void)isSimpleUniversalTravelRecords8FL1fAHaGN2VUY {
    NSLog(@"Check your Network");
}

- (void)isSimpleUniversalTravelRecordsNNK58BFziRyEy4BTdivRCl5nb {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleUniversalTravelRecordsrh54yfDh4JNoCCAIgd {
    NSLog(@"Get Info Success");
}

- (void)isSimpleUniversalTravelRecordsJXCY59j2BqyAnurO {
    NSLog(@"Continue");
}

- (void)isSimpleUniversalTravelRecordsMddwXMmdGAVQbxnnam {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleUniversalTravelRecordsEI1Mde6HYhgSngMaQtcNG88xPU {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleUniversalTravelRecordsfUm52ODE7dPMTt3n {
    NSLog(@"Get Info Success");
}

- (void)isSimpleUniversalTravelRecordsR6dSK4daElwzjJj1tP {
    NSLog(@"Continue");
}

- (void)isSimpleUniversalTravelRecordsDsHIh4cPEORlTsP1Oo5 {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleUniversalTravelRecordsW77MByoGGfKF {
    NSLog(@"Continue");
}

- (void)isSimpleUniversalTravelRecordsfpG782XVdbYW5PHB0zq {
    NSLog(@"Check your Network");
}

- (void)isSimpleCreativeWallpaperIn25TgPzOt5mzQ9geoA7A4R {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleCreativeWallpapergOaD0Qdq2oScDdJsVT90tlbAV {
    NSLog(@"Check your Network");
}

- (void)isSimpleCreativeWallpapertB8hWYtkVl4KlsBuX {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleCreativeWallpaperXt0YM9dA8rorlq3GtVHF5eW {
    NSLog(@"Get Info Success");
}

- (void)isSimpleCreativeWallpapertEJni0NjDciKMhApo2GdO1Tym {
    NSLog(@"Continue");
}

- (void)isSimpleCreativeWallpaperEcubEcWwuDPjYC2s1JpY {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleCreativeWallpaperEE28dw62Ir3PCiOjjet0w {
    NSLog(@"Get Info Success");
}

- (void)isSimpleCreativeWallpaperpMExCYEh7czts5a {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleCreativeWallpaperSFsjE5YqJrWNvaUWRMF0aaAb {
    NSLog(@"Check your Network");
}

- (void)isSimpleCreativeWallpaper6eTaTBcPd5s3090WqpCnsol {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleCreativeWallpaperhEjytv3IUbevJg {
    NSLog(@"Check your Network");
}

- (void)isSimpleCreativeWallpaperoqQf6aLKKywQ {
    NSLog(@"Continue");
}

- (void)isSimpleCreativeWallpaperX2Lxm2AE326xY3tnVdeFPOLc8 {
    NSLog(@"Get Info Success");
}

- (void)isSimpleCreativeWallpaperuqDwaDzDOptsOL {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleCreativeWallpapereipqmGnysolj6RBYzfTRY {
    NSLog(@"Check your Network");
}

- (void)isSimpleCreativeWallpaperznytHB9n4iQRcbpPn9t {
    NSLog(@"Continue");
}

- (void)isSimpleCreativeWallpaperC0lYJWynnqBh756M {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleCreativeWallpaper4SHteUz3udHx0mFe0nx712ofu1 {
    NSLog(@"Check your Network");
}

- (void)isSimpleCreativeWallpaper28iRaJY1oce0C0I3j6 {
    NSLog(@"Get Info Success");
}

- (void)isSimpleCreativeWallpaper8gIiOby6zvEc {
    NSLog(@"Continue");
}

- (void)isSimpleCreativeWallpaperQCUr7FU7cknp {
    NSLog(@"Continue");
}

- (void)isSimpleCreativeWallpaperzWwDyAE0d8PFAF169m6 {
    NSLog(@"Check your Network");
}
@end
